package com.september.aftermarket.rest;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.Key;
import com.google.code.morphia.query.Query;
import com.google.code.morphia.query.UpdateOperations;
import com.google.gson.Gson;
import com.september.mongo.data.DataProvider;
import com.september.mongo.entities.Salt;
import com.september.mongo.entities.User;
import com.september.prop.Properteer;
import com.september.util.BaseData;

@Path("/auth/")
public class AuthService {
	DataProvider provider = new DataProvider();
	Properties prop = Properteer.getInstance().Properties();
	
	
	
	@POST
	@Path("login")
	@Consumes(MediaType.APPLICATION_JSON)
	public <T extends BaseData> Response logIn(Map<String,String> map) {
		Gson gson = new Gson();
		try {
			User user = new User();
			boolean log = User.login(map.get("USER"), map.get("PASSWORD"));
			return Response.status(201).entity(gson.toJson(log)).build();
		} catch (Exception e) {
			Properteer.getInstance().getLogger(this.getClass()).error(e.getClass().getName(),e);
			return Response.status(201).entity(gson.toJson(false)).build();
		} 
		
	}
	
	@POST
	@Path("signup")
	@Consumes(MediaType.APPLICATION_JSON)
	public <T extends BaseData> Response signup(Map<String,Object> userPars) {
		Gson gson = new Gson();
		try {
			User user = (User) userPars.get("USER");
			String pwd = (String) userPars.get("PASSWORD");
			User signed = user.signup(user.getUserName(), pwd);
			return Response.status(201).entity(gson.toJson(signed)).build();
		} catch (Exception e) {
			Properteer.getInstance().getLogger(this.getClass()).error(e.getClass().getName(),e);
			return Response.status(201).entity(gson.toJson(null)).build();
		} 
		
	}
	
}
